<?php

namespace app\api\controller;


class Hospadvice
{
    private $pagesize = 10;

    /**
     * 添加
     *
     */
    public function add()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $name = trim($_REQUEST['name']);
//        if (empty($name)) {
//            $return['err'] = 10009;
//            $return['msg'] = '医嘱标题不能为空';
//            echo json_encode($return, true);
//            exit;
//        }

        $order_sn= trim($_REQUEST['order_sn']);
        if (empty($order_sn)) {
            $return['err'] = 10009;
            $return['msg'] = '订单号不能为空';
            echo json_encode($return, true);
            exit;
        }
        $orderModel = new \app\admin\model\HospOrder();
        $orderInfo = $orderModel->getDataForWhere(['order_sn'=>$order_sn]);
        if(empty($orderInfo))
        {
            $return['err'] = 10009;
            $return['msg'] = '订单不存在';
            echo json_encode($return, true);
            exit;
        }
        if($orderInfo['doctor_id']!=$userInfo['id'])
        {
            $return['err'] = 10009;
            $return['msg'] = '您无权给该订单添加医嘱';
            echo json_encode($return, true);
            exit;
        }

        $preface = trim($_REQUEST['preface']);

        $analysis = trim($_REQUEST['analysis']);
        $advise = trim($_REQUEST['advise']);
        $remarks = trim($_REQUEST['remarks']);
        $mlist = trim($_REQUEST['mlist']);

        $data = array(
            'uid' => $userInfo['id'],
            'hosp_id' => $_hosp,
            'name' => $name,
            'preface' => $preface,
            'order_sn' => $order_sn,
            'receive_uid' => $orderInfo['uid'],
            'receive_name'=>$orderInfo['uid_name'],
            'preface' => $preface,
            'analysis' => $analysis,
            'advise' => $advise,
            'mlist' => $mlist,
            'remarks' => $remarks,
            'is_send'=>1,
            'ctime' => time(),
            'utime' => time(),

        );

        $toolModel = new \app\admin\model\HospDoctorAdvice();
        $res = $toolModel->add($data);
        if ($res) {

            $return['err'] = 0;
            $return['msg'] = '操作成功';
            $return['data'] = $res;
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }


    /**
     * 医生发出的医嘱列表
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function doctorSendAdviceList()
    {
        $access_token = trim($_REQUEST['access_token']);
        $keywords = trim($_REQUEST['keywords']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
         $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        $w = array('is_delete' => 0,'is_send' => 1,  'hosp_id' => $_hosp,'uid'=>$userInfo['id']);
        if($keywords)
        {
            $w['receive_name']=array('like','%'.$keywords.'%');
        }
        $orderby = "id desc";

        $model = new \app\admin\model\HospDoctorAdvice();
        $lists = $model->lists($w, $this->pagesize, '*', $orderby);
        foreach ($lists['data'] as $key=>$val)
        {
            $lists['data'][$key]['ctimeFormat'] = date("Y-m-d",$val['ctime']);
        }

        $lists['data'] = changeUidToUserinfoForLists( $lists['data'],'receive_uid');
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }
    /**
     * 添加
     *
     */
    public function edit()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        $id = trim($_REQUEST['id']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
         $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $name = trim($_REQUEST['name']);
        if (empty($name)) {
            $return['err'] = 10009;
            $return['msg'] = '医嘱标题不能为空';
            echo json_encode($return, true);
            exit;
        }

        $order_sn= trim($_REQUEST['order_sn']);
        if (empty($order_sn)) {
            $return['err'] = 10009;
            $return['msg'] = '订单号不能为空';
            echo json_encode($return, true);
            exit;
        }
        $orderModel = new \app\admin\model\HospOrder();
        $orderInfo = $orderModel->getDataForWhere(['order_sn'=>$order_sn]);
        if(empty($orderInfo))
        {
            $return['err'] = 10009;
            $return['msg'] = '订单不存在';
            echo json_encode($return, true);
            exit;
        }
        if($orderInfo['doctor_id']!=$userInfo['id'])
        {
            $return['err'] = 10009;
            $return['msg'] = '您无权给该订单添加医嘱';
            echo json_encode($return, true);
            exit;
        }

        $toolModel = new \app\admin\model\HospDoctorAdvice();
        $oldAdviceInfo = $toolModel->getDataForWhere(['id'=>$id]);
        if(empty($oldAdviceInfo)){
            $return['err'] = 10009;
            $return['msg'] = '记录不存在';
            echo json_encode($return, true);
            exit;
        }
        if($oldAdviceInfo['uid']!=$userInfo['id'])
        {
            $return['err'] = 10009;
            $return['msg'] = '您无权操作该医嘱';
            echo json_encode($return, true);
            exit;
        }

        $preface = trim($_REQUEST['preface']);

        $analysis = trim($_REQUEST['analysis']);
        $advise = trim($_REQUEST['advise']);
        $mlist = trim($_REQUEST['mlist']);

        $data = array(
            'uid' => $userInfo['id'],
            'hosp_id' => $_hosp,
            'name' => $name,
            'preface' => $preface,
            'order_sn' => $order_sn,
            'receive_uid' => $orderInfo['uid'],
            'analysis' => $analysis,
            'advise' => $advise,
            'mlist' => $mlist,
            'utime' => time(),
        );


        $res = $toolModel->updateDataForWhere($data,['id'=>$oldAdviceInfo['id']]);
        if ($res) {
            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }


    public function del()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
         $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $id = trim($_REQUEST['id']);
        if(empty($id))
        {
            $return['err'] = 1;
            $return['msg'] = '参数错误';
            return json($return);
        }

        $toolModel = new \app\admin\model\HospDoctorAdvice();
        $oldToolInfo = $toolModel->getDataForWhere(['id'=>$id]);
        if(empty($oldToolInfo)){
            $return['err'] = 1;
            $return['msg'] = '您不能操作该记录';
            return json($return);
        }
        if($oldToolInfo['uid']!=$userInfo['id']){
            $return['err'] = 1;
            $return['msg'] = '您不能操作该记录';
            return json($return);
        }

        if($oldToolInfo['is_delete']==1){
            $return['err'] = 1;
            $return['msg'] = '记录已经删除';
            return json($return);
        }



        $data = array(
            'is_delete' => 1,
        );


        $res = $toolModel->updateDataForWhere($data,['id'=>$oldToolInfo['id']]);
        if ($res || $res==0) {

            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }



    /**
     *  医生获取订单列表
     */
    public function lists()
    {
        $access_token = trim($_REQUEST['access_token']);
        $status = trim($_REQUEST['status']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
         $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $pagesize = trim(input("pagesize"));//每页条数
        $ty = trim(input("ty"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        if(empty($ty)){
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $w = array('is_delete' => 0,  'hosp_id' => $_hosp,'ty'=>$ty,'uid'=>array('in',[0,$userInfo['id']]));
        $orderby = "sorts desc,id desc";

        $model = new \app\admin\model\HospDoctorTool();
        $lists = $model->lists($w, $this->pagesize, '*', $orderby);

       
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }
}

